/*
//  TextDownloaderViewController.m
//  TextDownloader
//
//  Created by Brennan Stehling on 10/25/09.
//  Copyright mwiphonesdk 2009. All rights reserved.
*/

#import "TextDownloaderViewController.h"
#import "TextDownloader.h"
#import "JSONDownloader.h"

@implementation TextDownloaderViewController

#pragma mark - Properties

@synthesize button;
@synthesize textview;

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.button = nil;
	self.textview = nil;
}

- (void)dealloc {
	[self.button release];
	[self.textview release];
    [super dealloc];
}

#pragma mark - Public

-(IBAction)downloadText {
	textview.text = @"";
	NSString *urlString = @"http://www.theresgonnabesnacks.com/data/data.txt";
	TextDownloader *td = [[TextDownloader alloc] init];
	[td downloadText:urlString delegate:self];	
	[td release];
}

-(IBAction)downloadJSON {
	textview.text = @"";
	NSString *urlString = @"http://www.theresgonnabesnacks.com/data/basic.json";
	JSONDownloader *jd = [[JSONDownloader alloc] init];
	[jd downloadJSON:urlString delegate:self];
	[jd release];
}

#pragma mark - Delegate Methods

- (void) downloadTextFinished: (NSString *) text {
	NSLog(@"Text: text.length = %d", text.length);
	textview.text = text;
}

- (void) downloadJSONFinished: (NSDictionary *) dictionary {
	NSLog(@"JSON: dictionary = %@", dictionary);
	textview.text = [NSString stringWithFormat:@"dictionary = %@", dictionary.description];
}

- (void) downloadTextFailed: (NSString *) errorMessage {
	NSLog(@"Text: errorMessage = %@", errorMessage);
	textview.text = [NSString stringWithFormat:@"errorMessage = %@", errorMessage];
}

- (void) downloadJSONFailed: (NSString *) errorMessage {
	NSLog(@"JSON: errorMessage = %@", errorMessage);
	textview.text = [NSString stringWithFormat:@"errorMessage = %@", errorMessage];
}

- (void) parsingJSONFailed: (NSString *) errorMessage {
	NSLog(@"JSON: errorMessage = %@", errorMessage);
	textview.text = [NSString stringWithFormat:@"errorMessage = %@", errorMessage];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES; // (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

@end
